<?php
namespace app\admin\controller;
use app\common;
use think\Controller;
use think\Session;
use think\Request;
use think\File;
use think\model;

class Qiye extends common
{
    public function index(){
        $where = '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $id = (isset($_GET['id'])) ? trim($_GET['id']) : '';
        //搜索条件

        if (strlen($id)) {
            $where = "id='{$id}'";
        }
        if ($keyword){
            $where = "title LIKE '%{$keyword}%' OR writer LIKE '%{$keyword}%' OR content LIKE '%{$keyword}%'";
        }
        //查询总数据
        $maxpages = Db('qiye')->where($where)->count('*');
        //最大页数
        $maxpage = $maxpages / 10;
        // 查询状态为0的数据 并且每页显示10条数据 总记录数为1000
        $list = db('qiye')->where($where)->paginate(20, $maxpages);
//        return $this->sucReturn($list);exit;
        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $this->assign('list', $list);
        //搜索占位
        $this->assign("id",$id);
        $this->assign("keyword",$keyword);
        $this->assign('loginname', $this->login());
        $this->assign('page', $page);
        $this->assign('maxpage', $maxpage);
        $this->assign('maxpages', $maxpages);
        $this->assign('empty', '<span class="empty">没有数据</span>');

        // 渲染模板输出
        return $this->fetch('qiye/index');
    }

    //加载添加企业页面
    public function add()
    {
        $this->assign('loginname', $this->login());
        return $this->fetch('qiye/add');
    }

    //添加数据
    public function insert()
    {
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['writer'] = $_POST['writer'];
        $data['status'] = $_POST['status'];
        $data['sort'] = $_POST['sort'];
        $data['add_time'] = time();
        $sql = db('qiye')->insert($data);
        if ($sql) {
            $this->success("添加成功", "qiye/index");
        } else {
            $this->error("数据添加失败");
        }
    }



    //加载修改页
    public function edit($id = 0)
    {
        //查找要修改的数据
        $info = db("qiye")->find($id);
        $this->assign('loginname', $this->login());
        $this->assign('row', $info);
        return $this->fetch("qiye/edit");
    }

    //将修改数据的数据保存到数据库

    public function update($id)
    {
        $data['title'] = $_POST['title'];
        $data['content'] = $_POST['content'];
        $data['writer'] = $_POST['writer'];
        $data['status'] = $_POST['status'];
        $data['sort'] = $_POST['sort'];
        $sql = db("qiye")->where("id", $id)->update($data);
        if ($sql > 0) {
            $this->success("修改成功", "qiye/index");
        } else {
            $this->error("修改失败");
        }

    }


    //删除数据
    public function delete($id)
    {
        $list = db("qiye")->find($id);
        if ($list > 0) {
            $sql = db('qiye')->where("id", $id)->delete();
            if ($sql) {
                $this->success("删除成功", "qiye/index");
            } else {
                $this->error("删除失败");
            }
        }
    }

}

